package com.i2bt.app.set;

import com.i2bt.R;
import com.i2bt.app.BaseActivity;
import com.i2bt.app.set.model.FileChooseAdapter;

import android.os.Bundle;
import android.os.Environment;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class FileManager extends BaseActivity implements OnItemClickListener {
	Button enter;
	TextView pathText;
	ListView list;

	private List<String> items = null;
	private List<String> paths = null;
	private String rootPath = "";
	private String curPath = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_set_path);
		File index = Environment.getExternalStorageDirectory();
		if (index.exists()) {
			rootPath = index.getPath();
			curPath = index.getPath();
		} else {
			File phone = Environment.getRootDirectory();
			System.out.println(phone.getPath().toString());
			rootPath = phone.getPath();
			curPath = phone.getPath();
			this.toastHandler("SD卡不存在，将存放在手机存储!");
		}
		initView();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub

		boolean result = super.onCreateOptionsMenu(menu);
		this.setActionMenuGroupVisible(false);
		this.setActionBarBack("设置");
		return result;
	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		if (arg0 == enter) {
			this.writeXML("SETTING", "SHOWPATH", pathText.getText().toString());
			this.writeXML("SETTING", "DOWNLOADPATH", curPath);
			finish();
		}
	}

	@Override
	protected void initView() {
		// TODO Auto-generated method stub
		pathText = (TextView) findViewById(R.id.set_choosePath_text);
		enter = (Button) findViewById(R.id.set_choosePath_Enter);
		list = (ListView) this.findViewById(R.id.set_choosePath_List);
		enter.setOnClickListener(this);
		list.setOnItemClickListener(this);
		getFileDir(rootPath);
	}

	@Override
	protected void logic() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position,
			long arg3) {
		// TODO Auto-generated method stub
		File file = new File(paths.get(position));
		if (file.isDirectory()) {
			curPath = paths.get(position);
			getFileDir(paths.get(position));
		} else {
			System.out.println("点击不是目录");
		}
	}

	private void getFileDir(String filePath) {
		System.out.println(filePath.substring(rootPath.length()));
		pathText.setText(filePath.substring(rootPath.length()));
		items = new ArrayList<String>();
		paths = new ArrayList<String>();
		File f = new File(filePath);
		File[] files = f.listFiles();
		for (int i = 0; i < files.length; i++) {
			File file = files[i];
			if (file.isDirectory() && !file.isHidden()) {
				items.add(file.getName());
				paths.add(file.getPath());
			}
		}
		list.setAdapter(new FileChooseAdapter(this, items, paths));
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (curPath.equals(rootPath)) {
				finish();
			} else {
				File f = new File(curPath);
				if (f.isDirectory()) {
					curPath = f.getParent();
					getFileDir(curPath);
				}
			}
		}
		return false;
	}

	@Override
	public void backHomeOperate() {
		// TODO Auto-generated method stub
		finish();
	}
}
